package com.damage.purchase_dept.oa.makePurchasePlan;

import com.damage.common.config.DepartmentConfig;
import com.damage.common.config.ProcessDefinitionConfig;
import com.damage.common.entity.PurchasePlan;
import com.damage.common.exception.IdentityException;
import com.damage.common.exception.IncorrectPriceException;
import com.damage.common.exception.ObsoleteException;
import com.damage.common.service.IWorkflowService;
import com.damage.purchase_dept.service.IPurchasePlanService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class StartMakePurchasePlanService{

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private IPurchasePlanService purchasePlanService;

    /**
     * 开启制作采购计划流程，返回流程实例
     * @param purchasePlan
     * @param variables
     * @return
     * @throws IncorrectPriceException
     * @throws ObsoleteException
     * @throws IdentityException
     */
    @Transactional
    public PurchasePlan startMakePurchasePlan(PurchasePlan purchasePlan)
            throws IncorrectPriceException, ObsoleteException, IdentityException {

        //验证身份
        User user = identityService.createUserQuery().userFirstName(purchasePlan.getPlanner()).singleResult();
        Group group = identityService.createGroupQuery().groupMember(user.getId()).singleResult();
        String groupId = group.getId();
        if (!DepartmentConfig.purchaseDept.equals(groupId)) {
            throw new IdentityException("采购计划制定人的身份不是采购部的员工，不能启动流程！");
        }
        identityService.setAuthenticatedUserId(user.getId());

        //保存采购计划
        String businessKey = null;
        try {
            businessKey = purchasePlanService.newPurchasePlan(purchasePlan).toString();
        } catch (IncorrectPriceException e) {
            throw e;
        } catch (ObsoleteException e) {
            throw e;
        }

        ProcessInstance processInstance = null;
        try {
            //启动流程
            processInstance = runtimeService.startProcessInstanceByKey(ProcessDefinitionConfig.MAKE_PURCHASE_PLAN, businessKey);
            String processInstanceId = processInstance.getId();
            purchasePlanService.setProcessInstanceId(purchasePlan.getId(), processInstanceId);
            log.info("start process of {key={}, bkey={}, pid={}}", new Object[]{"makePurchasePlan", businessKey, processInstanceId});
        } finally {
            identityService.setAuthenticatedUserId(null);
        }
        PurchasePlan result = purchasePlanService.findById(purchasePlan.getId());
        return result;
    }


}
